package com.blast.cn.persenter.check

import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.blast.cn.AppMainActivity
import com.blast.cn.ui.LoginActivity
import com.wxl.common.AppContext
import com.wxl.common.base.WorkPresenter
import com.wxl.common.serialport.SerialPortManager
import com.wxl.common.serialport.SerialPortUpdate
import com.wxl.common.util.ObservableUtil
import kotlinx.android.synthetic.main.activity_check_device_layout.view.*

class CheckDeviceSerialPortPresenter : WorkPresenter() {

    override fun onStartDoWork(vararg args: Any) {
        rootView.checkProgressSub.text = "正在检查设备串口,请勿进行其他操作..."
        SerialPortManager.openOnlyPort(object : SerialPortManager.Companion.OpenPortCallback {
            override fun onOpenPort(state: Int) {
                if (state == 0) {
                    checkUpdate()
                } else {
                    openPowerFailed(state)
                }
            }
        })
    }


    /**
     * 打开成功后检查控制板是否需要升级
     */
    private fun checkUpdate() {
        rootView.checkProgressSub.text = "检查控制板版本,请稍后..."
        rootView.checkProgressBar.progress = 60
        rootView.checkProgressText.text = "60%"
        SerialPortUpdate.startCheckUpdate(rootView.context,
            object : SerialPortUpdate.Companion.OnSerialPortUpdateCallback {
                override fun onCheckQBQSerialPortError(error: String) {
                    rootView.checkProgressSub.text = "升级控制板发生错误,请重试... $error"
                    rootView.retryBtn.visibility = View.VISIBLE
                }

                override fun onStartUpdateSerialPort() {
                    rootView.checkProgressSub.text = "正在升级控制板,请勿进行其他操作..."
                    rootView.checkProgressBar.progress = 65
                    rootView.checkProgressText.text = "65%"
                }

                override fun onFinishUpdateSerialPort() {
                    rootView.checkProgressSub.text = "升级检测完成,请稍等..."
                    rootView.checkProgressBar.progress = 100
                    rootView.checkProgressText.text = "100%"
                    checkEnd()
                }

                override fun onLoadUpdateFile() {
                    rootView.checkProgressSub.text = "正在解析升级文件,请勿进行其他操作..."
                    rootView.checkProgressBar.progress = 75
                    rootView.checkProgressText.text = "75%"
                }

                override fun onSendUpdateData(num: Int) {
                    var p = ++rootView.checkProgressBar.progress
                    rootView.checkProgressBar.progress = p
                    rootView.checkProgressText.text = "$p%"
                    rootView.checkProgressSub.text = "正在推送升级数据,请勿进行其他操作...   ${num + 1}/23"
                }

                override fun onSendInputData(num: Int) {
                    rootView.checkProgressSub.text = "正在写入数据,请勿进行其他操作...   ${num + 1}/23"
                }

            })
    }


    override fun onDestroy() {
        super.onDestroy()
        SerialPortManager.closePower()
        SerialPortManager.close()
    }


    /**
     * 自检结束
     */
    private fun checkEnd() {
        ObservableUtil.delay(1000, Runnable {
            val loginAccount = AppContext.appContext.getLoginAccount()
            if (TextUtils.isEmpty(loginAccount) || AppContext.appContext.checkLoginOutTime()) {
                rootView.context.startActivity(
                    Intent(
                        rootView.context,
                        LoginActivity::class.java
                    )
                )
            } else {
                rootView.context.startActivity(
                    Intent(
                        rootView.context,
                        AppMainActivity::class.java
                    )
                )
            }

            finish()
        })
    }


    private fun openPowerFailed(state: Int) {
        rootView.retryBtn.visibility = View.VISIBLE
        when (state) {
            -1 -> {
                rootView.checkProgressSub.text = "没有串口读/写权限。"
            }
            -2 -> {
                rootView.checkProgressSub.text = "打开控制板电源，发生未知错误。"
            }
            -3 -> {
                rootView.checkProgressSub.text = "打开控制板参数错误。"
            }
        }
    }
}